package com.example.demo.controller;


import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.base.config.auth.UserInfoShareHolder;
import com.example.demo.base.login.dto.UserCache;
import com.example.demo.base.login.service.ExamUserService;
import com.example.demo.domain.entity.ExamPortrait;
import com.example.demo.domain.entity.User;
import com.example.demo.dto.PortraitDto.ExamPortraitDto;
import com.example.demo.dto.ResponseDto;
import com.example.demo.exception.ValidatorException;
import com.example.demo.param.UserParam;
import com.example.demo.service.ExamPortraitService;
import com.example.demo.utils.JsonUtils;
import com.example.demo.utils.ValidatorUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户画像接口
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/exam/portrait")
public class ExamUserPortraitController {
    @Resource
    private ExamPortraitService examPortraitService;
    /**
     *
     * @param examPortraitDto
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public ResponseDto<ExamPortraitDto> query(@RequestBody ExamPortraitDto examPortraitDto) {
        System.out.println();
        UserCache userInfo = UserInfoShareHolder.getUserInfo();
        ValidatorUtil.require(examPortraitDto.getUserId(), "用户id");

        ValidatorUtil.require(examPortraitDto.getExamId(), "考试id");


        QueryWrapper<ExamPortrait> queryWrapper = new QueryWrapper<ExamPortrait>();
        queryWrapper.eq("exam_id", examPortraitDto.getExamId());
        queryWrapper.eq("user_id", examPortraitDto.getUserId());
        queryWrapper.eq("c_id", userInfo.getCompanyId());

        List<ExamPortrait> examPortraits = examPortraitService.getBaseMapper().selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(examPortraits)){
            ExamPortraitDto deserialize = JsonUtils.deserialize(examPortraits.get(0).getInfoStr(), ExamPortraitDto.class);
            return new ResponseDto().buildSucceedHasData(deserialize);
        }else {
            throw new ValidatorException("用户画像不存在，请联系管理员");
        }

    }






}
